# 森林防火规则函数
def hour_forest_fire_warning(row):
    # 火灾高风险：气温 > 30°C，风速 >= 4级，湿度 <= 40%
    if row['temp'] > 30 and row['wind_scale'] >= 4 and row['humidity'] <= 40:
        return '高风险'
    # 火灾中风险：气温 25-30°C，风速 >= 3级，湿度 <= 40%
    elif 25 <= row['temp'] <= 30 and row['wind_scale'] >= 3 and row['humidity'] <= 40:
        return '中风险'
    # 火灾低风险：气温 <= 25°C，风速 <= 2级，湿度 > 40%
    elif row['temp'] <= 25 and row['wind_scale'] <= 2 and row['humidity'] > 40:
        return '低风险'
    # 火灾无风险：降水量 > 0mm 或湿度 > 60%
    elif row['precipitation'] > 0 or row['humidity'] > 60:
        return '无风险'
    else:
        return '无风险'


def hour_forest_fire_warning_decision(row):
    return decision(row['fire_warning'])


def hour_forest_flood_warning_decision(row):
    return decision(row['flood_warning'])


def hour_forest_cold_warning_decision(row):
    return decision(row['cold_warning'])


def hour_forest_wind_warning_decision(row):
    return decision(row['wind_warning'])


def decision(warning):
    if warning == "高风险":
        return "紧急应对，严阵以待"
    elif warning == "中风险":
        return "加强巡查，防范未然"
    elif warning == "低风险":
        return "常规监控，保持警惕"
    else:
        return "常规监控，保持警惕"


# 洪涝灾害预警规则函数
def hour_flood_warning(row):
    # 洪涝高风险：降水量 >= 50mm，连续阴雨，风速 >= 4级
    if row['precipitation'] >= 50 and row['wind_scale'] >= 4:
        return '高风险'
    # 洪涝中风险：降水量 20-50mm，风速 >= 3级，湿度 >= 80%
    elif 20 <= row['precipitation'] < 50 and row['wind_scale'] >= 3 and row['humidity'] >= 80:
        return '中风险'
    # 洪涝低风险：降水量 < 20mm，风速 <= 2级，湿度适中
    elif row['precipitation'] < 20 and row['wind_scale'] <= 2 and row['humidity'] < 80:
        return '低风险'
    # 无降水，湿度较低
    else:
        return '无风险'


# 严寒预警规则
def hour_cold_warning(row):
    min_temp = row['temp']

    if min_temp <= -10:
        return '高风险'
    elif -10 < min_temp <= -5:
        return '中风险'
    elif -5 < min_temp <= 0:
        return '低风险'
    return '无风险'


# 大风预警规则
def hour_wind_warning(row):
    wind_scale = row['wind_scale']

    if wind_scale >= 6:
        return '高风险'
    elif 4 <= wind_scale < 6:
        return '中风险'
    elif wind_scale == 3:
        return '低风险'
    return '无风险'


# 森林防火预警规则
def days_forest_fire_warning(row):
    temp, wind, weather = row['max_temp'], row['wind_scale'], row['weather']

    # 仅根据温度、风速和天气来判断火灾风险
    if temp > 30 and wind >= 4 and (weather == '晴' or weather == '少云'):
        return '高风险'
    elif 25 <= temp <= 30 and wind >= 3 and (weather == '晴' or weather == '少云'):
        return '中风险'
    elif temp <= 25 and wind <= 2:
        return '低风险'
    return '低风险'


# 洪涝灾害预警规则
def days_flood_warning(row):
    wind, weather = row['wind_scale'], row['weather']

    # 根据风速和天气来判断洪涝风险
    if wind >= 4 and (weather == '多云转阴' or weather == '阴' or weather == '雪转阴'):
        return '高风险'
    elif wind >= 3 and (weather == '多云转阴' or weather == '阴'):
        return '中风险'
    elif wind <= 2 and (weather == '晴' or weather == '少云'):
        return '低风险'
    return '低风险'
